import React, { useContext, useState } from "react";
import { ACTON_OPTIONS } from "../../MsgQuickAction/type";
import { message } from "antd";
import { ChatContext } from "@/pages/BigScreen/context/store";
import { copyText } from "@/utils/common";

function useMsgContent() {
  const { reLoad, toPlay, toFeedback, handleActionLogList } =
    useContext(ChatContext);

  const [actionDialogVisible, setActionDialogVisible] = useState(false);
  const [actionFeedBackDialogVisible, setActionFeedBackDialogVisible] =
    useState(false);

  const [customized, setCustomized] = useState(false);
  const [pupData, setPupData] = useState<any>({});

  const [renderDom, setRenderDom] = useState<any>(null);

  const handleLongPress = (
    e: Event | null = null,
    item: any = {},
    dom?: any
  ) => {
    console.log("handleLongPress e", e);
    setActionDialogVisible(true);
    setRenderDom(dom);
    // e?.stopPropagation?.();
    if (e) {
      setPupData({
        event: e,
        item,
      });
    } else {
      setPupData({
        ...pupData,
        item,
      });
    }
  };

  const handleFeedBackDownClick = (item: any) => {
    setPupData({
      item,
    });
    setActionDialogVisible(false);
    setActionFeedBackDialogVisible(true);
  };

  const sendFeedBackDownClick = (str?: string) => {
    toFeedback({
      feedback: ACTON_OPTIONS.thumbsDown,
      id: pupData?.item?.id,
      feedbackDescription: str || "",
    });
    setActionDialogVisible(false);
    setActionFeedBackDialogVisible(false);
  };

  const handleClick = (item: IMsgActionItem) => {
    const { item: itemData = {} } = pupData;
    const content = itemData?.messageBody?.contents?.[0]?.content ?? "";
    if (item.key === "copy") {
      copyText(content).then(() => {
        message.success("复制成功");
      });
    } else if (item.key === ACTON_OPTIONS.log) {
      handleActionLogList(itemData.msgId);
    } else if (item.key === ACTON_OPTIONS.reload) {
      reLoad(itemData);
    } else if (item.key === ACTON_OPTIONS.play) {
      toPlay(itemData);
    } else if (item.key === ACTON_OPTIONS.thumbsUp) {
      if (itemData?.feedback === ACTON_OPTIONS.thumbsUp) {
        return;
      }
      toFeedback({
        feedback: ACTON_OPTIONS.thumbsUp,
        id: itemData.id,
      });
    } else if (item.key === ACTON_OPTIONS.thumbsDown) {
      if (itemData?.feedback === ACTON_OPTIONS.thumbsDown) {
        return;
      }
      toFeedback({
        feedback: ACTON_OPTIONS.thumbsDown,
        id: itemData.id,
      });
    } else {
      message.success("操作成功");
    }
  };

  const handleActionDialogVisibleCancel = () => {
    setActionDialogVisible(false);
  };
  const handleFeedBackDialogVisibleCancel = () => {
    setActionFeedBackDialogVisible(false);
  };

  return {
    customized,
    pupData,
    actionDialogVisible,
    renderDom,
    actionFeedBackDialogVisible,

    handleLongPress,
    setCustomized,
    handleClick,
    handleActionDialogVisibleCancel,
    handleFeedBackDialogVisibleCancel,
    handleFeedBackDownClick,
    sendFeedBackDownClick,
  };
}

export default useMsgContent;
